home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / clipplanes / cutout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  4.7 KB  |  201 lines

  1. /*
  2.  * Copyright 1993, 1995, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* cutout.c
  19.  *     This program demonstrates how to use multiple  
  20.  *     clipping planes to cut a hole out of an object.
  21.  *
  22.  *     ESCAPE key    - exits program
  23.  */
  24. #include <GL/gl.h>
  25. #include <GL/glu.h>
  26. #include <GL/glut.h>
  27.  
  28. #include <math.h>
  29. #include <stdio.h>
  30.  
  31. /* Function Prototypes */
  32.  
  33. GLvoid  initgfx( GLvoid );
  34. GLvoid  drawScene( GLvoid );
  35. GLvoid  reshape( GLsizei, GLsizei );
  36. GLvoid  animate( GLvoid );
  37. GLvoid  visibility( GLint );
  38. GLvoid  keyboard( GLubyte, GLint, GLint );
  39.  
  40. GLvoid  drawTorusRing( GLboolean );
  41.  
  42. void printHelp( char * );
  43.  
  44. /* Global Definitions */
  45.  
  46. #define KEY_ESC    27    /* ascii value for the escape key */
  47.  
  48. /* Global Variables */
  49.  
  50. static GLfloat angle = 0.0; 
  51.  
  52. GLvoid
  53. main( int argc, char *argv[] )
  54. {
  55.     GLsizei  width, height, winSize;
  56.  
  57.     glutInit( &argc, argv );
  58.  
  59.     width = glutGet(GLUT_SCREEN_WIDTH); 
  60.     height = glutGet(GLUT_SCREEN_HEIGHT);
  61.     winSize = ( width < height ) ? width : height;
  62.     glutInitWindowPosition( winSize/4, winSize/4 ); 
  63.     glutInitWindowSize( winSize/2, winSize/2 );
  64.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  65.     glutCreateWindow( argv[0] );
  66.     
  67.     initgfx();
  68.  
  69.     glutKeyboardFunc( keyboard );
  70.     glutReshapeFunc( reshape );
  71.     glutIdleFunc( animate ); 
  72.     glutVisibilityFunc( visibility ); 
  73.     glutDisplayFunc( drawScene ); 
  74.  
  75.     printHelp( argv[0] );
  76.     glutMainLoop();
  77. }
  78.  
  79. GLvoid
  80. printHelp( char *progname )
  81. {
  82.     fprintf(stdout, "\n%s - demonstrates how to use multiple "
  83.         "clipping planes\nto create a cutout in a sphere.\n\n"
  84.         "Escape key    - exit the program \n\n",
  85.         progname );
  86. }
  87.  
  88. void
  89. initgfx( void )
  90. {
  91.     GLfloat front_diffuse[] = { 1.0, 0.1, 0.1, 1.0};
  92.     GLfloat back_diffuse[] = { 0.1, 1.0, 0.1, 1.0};
  93.     GLfloat mat_specular[] = { 0.8, 0.8, 0.8, 1.0 };
  94.     GLfloat mat_shininess[] = { 40.0 };
  95.  
  96.     glMaterialfv(GL_FRONT, GL_DIFFUSE, front_diffuse);
  97.     glMaterialfv(GL_BACK, GL_DIFFUSE, back_diffuse);
  98.  
  99.     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
  100.     glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
  101.  
  102.     glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, 1.0);
  103.  
  104.     glEnable( GL_LIGHTING );
  105.     glEnable( GL_LIGHT0 );
  106.  
  107.     glClearColor( 0, 0, 0, 1 );
  108.     glEnable( GL_DEPTH_TEST );
  109. }
  110.  
  111. GLvoid 
  112. keyboard( GLubyte key, GLint x, GLint y )
  113. {
  114.     switch (key) {
  115.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  116.         exit(0);
  117.     }
  118. }
  119.  
  120. GLvoid
  121. reshape( GLsizei width, GLsizei height )
  122. {
  123.     GLdouble    aspect;
  124.  
  125.     glViewport( 0, 0, width, height );
  126.  
  127.     aspect = (GLdouble) width / (GLdouble) height;
  128.  
  129.     glMatrixMode( GL_PROJECTION );
  130.     glLoadIdentity();
  131.     gluPerspective( 45.0, aspect, 3.0, 13.0 );
  132.     glMatrixMode( GL_MODELVIEW );
  133.     glLoadIdentity();
  134.     glTranslatef( 0.0, 0.0, -8.0 ); 
  135. }
  136.  
  137. GLvoid
  138. animate( GLvoid )
  139. {
  140.     /* update the current angle */
  141.     angle = fmodf( (angle - 1.0), 360.0 );
  142.  
  143.     /* Tell GLUT to redraw the scene */
  144.     glutPostRedisplay();
  145. }
  146.         
  147. GLvoid
  148. visibility( int state )
  149. {
  150.     if (state == GLUT_VISIBLE) {
  151.         glutIdleFunc( animate );
  152.     } else {
  153.         glutIdleFunc( NULL );
  154.     }
  155. }
  156.  
  157. GLvoid
  158. drawScene( GLvoid )
  159. {
  160.     /* clips away all but right hand wedge when enabled */
  161.     GLdouble right[4] = { 1.0, 0.0, 0.0, -0.5 };
  162.     /* clips away all but left hand wedge when enabled */
  163.     GLdouble left[4]  = { -1.0, 0.0, 0.0, -0.5 };
  164.     GLdouble bottom[4] = { 0.0, 1.0, 0.0, -0.5 };
  165.     GLdouble top[4]  = { 0.0, -1.0, 0.0, -0.5 };
  166.  
  167.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  168.  
  169.     glPushMatrix(); 
  170.         glRotatef( angle, 0.0, 1.0, 0.0 );
  171.  
  172.         /* define clipping planes fixed in scene */
  173.         glClipPlane (GL_CLIP_PLANE0, left);
  174.         glClipPlane (GL_CLIP_PLANE1, right);
  175.         glClipPlane (GL_CLIP_PLANE2, top);
  176.         glClipPlane (GL_CLIP_PLANE3, bottom);
  177.         
  178.         /* clip parts of the solid sphere */
  179.         glEnable( GL_CLIP_PLANE0 );
  180.             glutSolidSphere(1.5, 15, 15);
  181.         glDisable( GL_CLIP_PLANE0 );
  182.  
  183.         glEnable( GL_CLIP_PLANE1 );
  184.             glutSolidSphere(1.5, 15, 15);
  185.         glDisable( GL_CLIP_PLANE1 );
  186.  
  187.         glEnable( GL_CLIP_PLANE2 );
  188.             glutSolidSphere(1.5, 15, 15);
  189.         glDisable( GL_CLIP_PLANE2 );
  190.  
  191.         glEnable( GL_CLIP_PLANE3 );
  192.             glutSolidSphere(1.5, 15, 15);
  193.         glDisable( GL_CLIP_PLANE3 );
  194.  
  195.         glutSolidSphere(0.3, 15, 15);
  196.  
  197.     glPopMatrix();
  198.  
  199.     glutSwapBuffers();
  200. }
  201.